package com.example.pwj.ffstudy.ui.zhihu.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.pwj.ffstudy.R;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyCommentSection;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyExtra;
import com.example.pwj.ffstudy.ui.fragment.ContainerFragment;
import com.example.pwj.ffstudy.ui.zhihu.ZhiConst;
import com.example.pwj.ffstudy.ui.zhihu.adapter.ZNewsCommAdapter;
import com.example.pwj.ffstudy.ui.zhihu.contract.ZNewsCommContract;
import com.example.pwj.ffstudy.ui.zhihu.di.DaggerZNewsPComponent;
import com.example.pwj.ffstudy.ui.zhihu.presenter.ZNewsCommPresenter;
import com.example.pwj.ffstudy.util.EventBusUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by pwj on 2017/8/28.
 */

public class ZNewsCommFragment extends ContainerFragment implements ZNewsCommContract.View {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Inject
    ZNewsCommPresenter mPresenter;
    private ZNewsCommAdapter mAdapter;
    private ZDailyExtra zDailyExtra;
    private boolean isExpand;
    private int story_id;

    @Override
    protected void initFm() {
        Bundle arguments = getArguments();
        story_id = arguments.getInt(ZhiConst.ZHIHU_ID, 0);
        zDailyExtra = arguments.getParcelable(ZhiConst.ZDAILY_EXTRA);
        mActionBar.setTitle(zDailyExtra.getComments() + "条点评");
        EventBusUtils.register(this);
        DaggerZNewsPComponent.builder()
                .build()
                .inject(this);
        mPresenter.takeView(this);
        mAdapter = new ZNewsCommAdapter(mContext);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter);
        mPresenter.loadZNewsLongComment(story_id, zDailyExtra.getLong_comments() + "条长评"
                , zDailyExtra.getShort_comments() + "条短评");
        //不搞分页加载用take操作符也是每次取所有的20条
       /* mAdapter.setOnLoadMoreListener(new CommAdapter.OnLoadMoreListener() {
            @Override
            public void loadMore() {
                if (isExpand) {
                    LogUtils.e("loadmore");
                    mPresenter.loadZNewsShortComment(story_id,true);
                }
            }
        });*/
    }

    @Override
    public void onLoadZNewsLongComment(List<ZDailyCommentSection> sections) {
        mAdapter.setNewData(sections);
        mAdapter.loadComplete();
    }

    @Override
    public void onLoadZNewsShortComment(List<ZDailyCommentSection> sections) {
//        mAdapter.loadComplete();
        mAdapter.expand(sections);
    }

    @Override
    public void onLoadMoreZNewsComment(List<ZDailyCommentSection> sections) {
//        mAdapter.loadComplete();
        mAdapter.addExpand(sections);
    }

    @Override
    public void onError() {

    }

    @Override
    public void onComplete() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_znews_comment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusUtils.unregister(this);
        if (mPresenter != null) {
            mPresenter.dropView();
            mPresenter = null;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HashMap<String,Boolean> map){
        isExpand = map.get(ZhiConst.COMMENT_IS_EXPAND);
        if (isExpand) {
            mPresenter.loadZNewsShortComment(story_id, false);
        }
    }
}
